package com.sharp.framework.utils;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Title: PinYinUtil
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/8/20 15:02
 */
public class PinYinUtil {

    private static final String CHINESE_WORD = "[\\u4E00-\\u9FA5]+";
    private static final String NUMBER = "[0-9]+";
    private static final String[] CHINESE_WORD_NUMBER = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static HanyuPinyinOutputFormat defaultFormat = null;

    public static String replaceNumber(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        char[] chars = str.toCharArray();
        StringBuilder res = new StringBuilder();
        // 获取所有的汉字的拼音
        for (char c : chars) {
            if (Character.toString(c).matches(NUMBER)) {
                res.append(CHINESE_WORD_NUMBER[c - '0']);
            } else {
                res.append(c);
            }
        }
        return res.toString();
    }

    public static String getSimplePy(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        char[] chars = str.toCharArray();
        StringBuilder res = new StringBuilder();
        // 获取所有的汉字的拼音
        for (char c : chars) {
            String py = getSimplePy(c);
            if (py != null) {
                res.append(py);
            }
        }
        return res.toString();
    }

    public static String getSimplePy(char c) {
        if (!Character.toString(c).matches(CHINESE_WORD)) {
            return Character.toString(c);
        }
        HanyuPinyinOutputFormat format = getFormat();
        try {
            // 获取拼音
            String py = PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
            if (StringUtils.isNotBlank(py)) {
                return py.substring(0, 1);
            } else {
                return py;
            }

        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return null;
        }
    }

    private static HanyuPinyinOutputFormat getFormat() {
        if (defaultFormat == null) {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            // 小写
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            // 声调不要
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
            defaultFormat = format;
            return format;
        }
        return defaultFormat;
    }

    public static List<String> getPyStr(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        char[] chars = str.toCharArray();
        List<String> pyList = new ArrayList<>();
        // 获取所有的汉字的拼音
        for (char c : chars) {
            String py = getPy(c);
            if (py != null) {
                pyList.add(py);
            }
        }
        return pyList;
    }

    public static String getPy(char c) {
        if (!Character.toString(c).matches(CHINESE_WORD)) {
            return Character.toString(c);
        }
        HanyuPinyinOutputFormat format = getFormat();
        try {
            // 获取拼音
            return PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            return null;
        }
    }

    public static List<Set<String>> getPySetStr(String str) {
        if (str == null) {
            return null;
        }

        char[] chars = str.toCharArray();
        List<Set<String>> pyList = new ArrayList<>();
        Set<String> pySet = null;
        // 获取所有的汉字的拼音
        for (char c : chars) {
            pySet = getPySet(c);
            if (pySet != null) {
                pyList.add(pySet);
            }
        }

        return pyList;
    }

    public static Set<String> getPySet(char c) {
        HanyuPinyinOutputFormat format = getFormat();
        String[] pyList = null;
        try {
            // 获取拼音
            pyList = PinyinHelper.toHanyuPinyinStringArray(c, format);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }

        Set<String> pySet = null;
        // 发生异常或者字符不是拼音的时候，pins都有可能是null，所以，必须进行判断
        if (pyList != null) {
            pySet = new HashSet<>();
            for (String pinyin : pyList) {
                pySet.add(pinyin);
            }
        }

        return pySet;
    }
}
